package cate.game.pvp.throne.data;

import cate.common.table.d.GDThrone;
import cate.common.util.XT;
import cate.game.pvp.throne.data.ko.TBKoReport;
import cate.game.pvp.throne.dto.TBBaseDTO;
import cate.game.pvp.throne.dto.TBBossDTO;
import cate.game.pvp.throne.dto.TBGroupDataDTO;
import cate.game.pvp.throne.dto.TBKoBaseDTO;
import cate.game.pvp.throne.msg.TBAllDataNotice;
import cate.game.pvp.throne.msg.TBKoBetNotice;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TBCrossDataHolder {

	public TBBaseDTO base;

	public TBKoBaseDTO koBase;

	public TBBossDTO boss;

	@NoteField("各个分组的数据")
	public Map<Integer, TBGroupDataDTO> groupMap = new HashMap<>();

	public TBCrossDataHolder() {
	}

	@JsonIgnore
	public TBGroupDataDTO getGroup(int group) {
		return groupMap.get(group);
	}

	public void synAllData(TBAllDataNotice data) {
		this.base = data.base;
		syncKoBase(data.koBase);
		syncBoss(data.boss);
		syncGroupPart(data.groupMap);
	}


	public void syncKoBase(TBKoBaseDTO koBase){
		this.koBase = koBase;
	}

	public void syncKoBet(TBKoBetNotice notice){
		koBase.bet.cur.update(notice.simple);
	}

	public void syncBoss(TBBossDTO boss){
		this.boss = boss;
	}

	public void syncGroupPart(Map<Integer, TBGroupDataDTO> groupMap) {
		for (TBGroupDataDTO value : groupMap.values()) {
			TBGroupDataDTO old = getGroup(value.levelGroup);
			if (old == null) {
				old = new TBGroupDataDTO(value.levelGroup);
				this.groupMap.put(value.levelGroup, old);
			}
			if (value.trial != null) {
				old.syncTrial(value.trial);
			}
			if (value.ko != null) {
				old.ko = value.ko;
			}
		}
	}

	public List<TBKoReport> findRoleReports(Role role) {
		TBGroupDataDTO groupData = getGroup(role.getPvp().throne.levelGroup);
		if (groupData != null) {
			return groupData.ko.findRoleReports(role.getUid());
		}
		return new ArrayList<>();
	}

	public boolean allowChangeKoPs(Role role) {
		if (!base.timeTable.inKo()) {
			return false;
		}
		List<TBKoReport> reports = findRoleReports(role);
		if (XT.isBlank(reports)) {
			return false;
		}
		final int round = koBase.timeTable.getRound();
		final byte period = koBase.timeTable.getPeriod();
		if (round == 0 || round == 4 && period == GDThrone.KoPeriod.REST) {
			return true;
		}
		return false;
	}

}
